//
//  ZLW_NetWorkRequestManager.m
//  EPA
//
//  Created by lanou3g on 16/4/15.
//  Copyright © 2016年 zlw. All rights reserved.
//

#import "ZLW_NetWorkRequestManager.h"
//延展,存储 block 属性
@interface ZLW_NetWorkRequestManager ()

/**
 *  存储 block 属性
 */
@property (nonatomic, copy) ResultBlock blcok;

@end

@implementation ZLW_NetWorkRequestManager
//单例方法
+ (instancetype)defailtManager
{
    static ZLW_NetWorkRequestManager *manager = nil;
    @synchronized(self) {
        if (!manager) {
            manager=[[ZLW_NetWorkRequestManager alloc] init];
        }
    }
    return manager;
}

+(void)requestURL:(NSURL *)url Method:(URLRwquestMethod)method Params:(NSDictionary *)param Result:(ResultBlock)resultBlock
{
    //空的可变字符串
    NSMutableString *urlString = [NSMutableString new];
    if ([url isKindOfClass:[NSURL class]])
    {
        //拼接 url
        [urlString appendString:url.absoluteString];
    }
    else if ([url isKindOfClass:[NSString class]])
    {
        [urlString appendString:(NSString *)url];
    }
    if (urlString.length == 0)
    {
        NSError *error = [NSError errorWithDomain:@"EPA" code:251 userInfo:@{@"reson":@"你输入网址不对"}];
        resultBlock(nil,nil,error);
    }
    //把参数字典转换为字符串
    ZLW_NetWorkRequestManager *manager = [ZLW_NetWorkRequestManager new];

    NSString *paraStr = [manager strFromDict:param];
    
    switch (method) {
        case CK_GET:
        {
            if (paraStr.length > 0) {
                [urlString appendFormat:@"?%@",paraStr];
            }
            [urlString appendString:paraStr];
            [manager GETrequestURL:[NSURL URLWithString:urlString]resultBlock:resultBlock];
       
            break;
        }
        case CK_POST:
        {
            [manager PostrequstURL:urlString ParaStr:paraStr resultBlock:resultBlock];
            break;
        }
        default:
            break;
    }
    
}

//GET网路请求
- (void)GETrequestURL: (NSURL *)url resultBlock:(ResultBlock)blcok
{
  
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]dataTaskWithURL:url completionHandler:blcok];

    [dataTask resume];
}

//POST 网路请求
- (void)PostrequstURL: (NSString *)urlStr ParaStr:(NSString *)parastr resultBlock:(ResultBlock)blcok
{
    //创建 NSURL
    NSURL *url = [NSURL URLWithString:urlStr];
    //创建可变请求
    NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url];
    //设置 POST
    [mRequest setHTTPMethod:@"POST"];
    //请求体
    [mRequest setHTTPBody:[parastr dataUsingEncoding:NSUTF8StringEncoding]];
    
    NSURLSessionDataTask  *dataTask = [[NSURLSession sharedSession]dataTaskWithRequest:mRequest completionHandler:blcok];
    [dataTask resume];
}

//字典转换为字符串
-(NSString *)strFromDict:(NSDictionary *)dict
{
    //空字符串
//    NSMutableString *paraStr = [NSMutableString stringWithFormat:@"?"];
    //空数组
    NSMutableArray  *array = [NSMutableArray new];
    for (NSString *key in dict) {
        NSString *str = [NSString stringWithFormat:@"%@=%@",key,dict[key]];
        [array addObject:str];
    }
    //数组拼接字符串
    NSString *para = [array componentsJoinedByString:@"&"];
  
    //返回
//    [paraStr appendString:para];
    return para;
}

@end
